<template>
  <Carousel autoplay :autoplay-speed="3000" loop>
    <template v-if="show">
      <CarouselItem v-for="item in cars" :key="item.id">
        <div class="demo-carousel" align="center">
          <a :href="item.url" target="_blank">
            <img class="c-image" :src="item.imageBase" :alt="item.title">
          </a>
        </div>
      </CarouselItem>
    </template>
  </Carousel>
</template>

<style scoped>
.c-image {
  width: 900px;
  height: 360px;
  border-radius: 5px;
}
</style>

<script>
  export default {
    data() {
      return {
        show: false,
        cars: [],
      };
    },
    methods: {
      init() {
        this.$axios({
          method: 'get',
          url: '/api/carousel',
          params: {
            isActive: '1'
          }
        }).then(res => {
          this.cars = res.data.result;
          this.loadImages(this.cars);
        })
      },
      loadImages(list) {
        let imageIds = [];
        list.forEach(e => imageIds.push(e.imageId));
        this.$axios({
          method: "post",
          url: "/api/image",
          data: {
            imageIds: imageIds
          }
        }).then((res) => {
          let imageList = {};
          res.data.forEach(e => imageList[e.id] = e.imageBase);
          this.cars.forEach(e => this.$set(e, 'imageBase', imageList[e.imageId]));
          this.show = true;
        })
      }
    },
    mounted() {
      this.init();
    }
  };
</script>
